package filesystem;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.eclipse.emf.common.notify.Adapter;

import filesystem.adapters.IconAdapter;
import filesystem.adapters.JLabelPropertyAdapter;

/**
 * Main application class for staring UI
 * 
 * @author Renat Zubairov, Igor Drobiazko
 */
public class Start {

	private static final File file = FilesystemFactory.eINSTANCE.createFile();

	/**
	 * Create the GUI and show it. For thread safety, this method should be
	 * invoked from the event-dispatching thread.
	 */
	private static void createAndShowGUI() {
		// Create and set up the window.
		final JFrame frame = new JFrame("EMF4Swing tutorial 1");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(createButtonPanel(frame), BorderLayout.SOUTH);
		panel.add(createLabelsPanel(), BorderLayout.CENTER);
		frame.setContentPane(panel);

		// Display the window.
		frame.pack();
		frame.setSize(380, 100);
		centerFrame(frame);
		frame.requestFocus();
		frame.setVisible(true);
	}

	/**
	 * This method creates a set of labels for showing
	 * different object properties
	 * 
	 * @return
	 */
	private static Component createLabelsPanel() {
		JPanel panel = new JPanel(new FlowLayout());
		JLabel fileLabel = new JLabel();
		// Adapter for showing file name
		Adapter nameLabelAdapter = new JLabelPropertyAdapter(fileLabel, FilesystemPackage.FILE__NAME);
		file.eAdapters().add(nameLabelAdapter);
		// Adapter for showing icon
		IconAdapter iconAdapter = new IconAdapter(fileLabel);
		file.eAdapters().add(iconAdapter);
		panel.add(fileLabel);
		return panel;
	}

	/**
	 * Creates panel with buttons
	 * 
	 * @return
	 */
	private static Component createButtonPanel(final JFrame frame) {
		JPanel result = new JPanel(new FlowLayout());
		JButton button = new JButton("Change name");
		button.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				String newName = JOptionPane.showInputDialog(frame,
						"Please enter a new file name", file.getName());
				if (newName != null && newName.trim().length() > 0) {
					file.setName(newName);
				}
			}

		});
		result.add(button);
		JButton button2 = new JButton("Switch file type");
		button2.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				int newIntValue = (file.getType().getValue() + 1) % 3;
				file.setType(FileType.get(newIntValue));
			}

		});
		result.add(button2);
		return result;
	}

	/**
	 * Centers given frame
	 * 
	 * @param frame
	 */
	private static void centerFrame(JFrame frame) {
		Dimension dim = frame.getToolkit().getScreenSize();
		Rectangle abounds = frame.getBounds();
		frame.setLocation((dim.width - abounds.width) / 2,
				(dim.height - abounds.height) / 2);
	}

	/**
	 * Main method, constructing and showing main GUI Frame
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		file.setName("Test file name");
		file.setHidden(false);
		file.setType(FileType.TEXT);
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}

}
